package com.ag.work2.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.ag.common.AnnexPath;
import com.ag.common.BaseAction;
import com.ag.hmk.service.impl.WordToHtmlSerImpl;
import com.ag.lzm.dao.TbUriDAO;
import com.ag.lzm.utils.PlanAnnex;
import com.ag.lzm.utils.PlanUtils;
import com.opensymphony.xwork2.ActionContext;

public class Upload5Action extends BaseAction implements SessionAware {
	private static final long serialVersionUID = 1474187815517066686L;

	@Resource(name = "WordToHtmlSerImpl")
	private WordToHtmlSerImpl wthsi;
	@Resource(name = "LZMTbUriDAO")
	private TbUriDAO uriDAO;

	private List<File> Filedata;
	private List<String> FiledataFileName;
	private List<String> FiledataContentType;
	private String name;

	private Map<String, Object> session;

	public String doUpload() {
		ActionContext ac = ActionContext.getContext();
		HttpServletRequest request = ServletActionContext.getRequest();
		System.out.println(request.getAttribute("name"));
		System.out.println(request.getParameter("name"));

		// ServletContext sc = (ServletContext) ac
		// .get(ServletActionContext.SERVLET_CONTEXT);
		// String savePath = sc.getRealPath("/");
		// savePath = savePath + "uploads/";
		// File f1 = new File(savePath);
		// if (!f1.exists()) {
		// f1.mkdirs();
		// }

		/**
		 * 自定义硬盘映射路径
		 */
		// String fileUL ="F:MYTEST/";
		String fileUL = uriDAO.findById(AnnexPath.PROPOSE_ANNEX_PATH)
				.getUPath();
		//System.out.println();
		System.out.println("save_plan.jsp" + fileUL);
		File f2 = new File(fileUL);
		if (!f2.exists()) {
			f2.mkdirs();
		}
		/**
		 * 获取上传文件数量
		 */
		int size = Filedata.size();
		if (size == 0)
			return ERROR;
		String extName = null;
		String name = null;
		for (int i = 0; i < size; i++) {
			// System.out.println("FiledataFileName:" +
			// FiledataFileName.get(i));
			extName = FiledataFileName.get(i).substring(
					FiledataFileName.get(i).lastIndexOf("."));
			name = UUID.randomUUID().toString();
			// addAnnex2Session(name + extName, FiledataFileName.get(i));
			// File file = new File(savePath + name + extName);

			File file = new File(fileUL + name + extName);
			try {
				FileUtils.copyFile(Filedata.get(i), file);
			} catch (IOException e) {
				e.printStackTrace();
			}
			// System.out.println(name+"name");
			// System.out.println(extName+"extName");
//			String u1 = fileUL + name + extName;
//			String u2 = fileUL + name + ".html";
			// System.out.println("u1"+u1);
			// System.out.println("u2"+u2);
			//wthsi.wordToHtml(u1, u2);
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			response.getWriter().print(name + extName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<File> getFiledata() {
		return Filedata;
	}

	public void setFiledata(List<File> filedata) {
		Filedata = filedata;
	}

	public List<String> getFiledataFileName() {
		return FiledataFileName;
	}

	public void setFiledataFileName(List<String> filedataFileName) {
		FiledataFileName = filedataFileName;
	}

	public List getFiledataContentType() {
		return FiledataContentType;
	}

	public void setFiledataContentType(List<String> filedataContentType) {
		FiledataContentType = filedataContentType;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getAllFile() {
		return null;
	}

}
